import defaultImg from '@/components/defaultImg/defaultImg.vue' //默认图片组件
export default {
  components: {
    defaultImg
  },
  data() {
    return {
      isWelfare: false, //是否显示绑定爱车弹窗
      specifAllbrand: {
        brandlist: [], //品牌
        Serieslist: [], //系列
        Displacement: [], //排量
        Modelyear: [], //年款
        Configulist: [], //车子
      },
      mycarlist: [], //我的爱车列表
      defaultImg: false, //默认图
      carnumber: 0,
      flowershow: false, //lodding
      type: 0, //我的进入110,福利购进入100,其他进入120
      carNum: '', //车牌号
      couponId: '', //优惠券id
      carInfo: {}, //当前选中爱车信息
      welfareTitle: '', //福利购标题
      isWxx: false,
      showPage: false,
    }
  },
  onShow() {
    //#ifdef H5
    let ua = window.navigator.userAgent.toLowerCase()
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
      this.isWxx = true
    } else {
      this.isWxx = false
    }
    //#endif
    this.GetCarList()
  },
  onLoad(caridxix) {
    if (caridxix.type === '100') {
      this.getWelfareInfo(caridxix)
    }

    this.type = caridxix.type

    // 选择品牌，系列，排量，年款，车子
    uni.$on('mylovecar', data => {
      Object.assign(this.specifAllbrand, data)
      console.log('this.specifAllbrand', this.specifAllbrand)
      if (data.licenseplate && data.pageFrom === 'mylovecar') {
        //添加爱车
        this.AddCar()
      }
    })
    let datatrue = uni.getStorageSync('Ztc_C_USER_INFO')
    if (!datatrue.isNew) {
      // #ifdef APP-PLUS
      let webview = this.$mp.page.$getAppWebview()
      webview.setTitleNViewButtonStyle(0, {
        text: '',
        width: '0px'
      })
      // #endif
      // #ifdef H5
      this.$nextTick(() => {
        let view = document.querySelectorAll('.uni-page-head-ft .uni-page-head-btn')
        console.log('view', view)
        view[0].childNodes[0].innerHTML = ''
        view[0].style.width = '0'
      })
      // #endif
    }
  },
  onUnload() {
    uni.$off('mylovecar')
  },
  created() {

  },
  methods: {
    //获取优惠券id
    async getCouponIdByBuyDiscountsOrderId(orderId) {
      try {
        let data = {
          'buyDiscountsId': orderId
        }
        const res = await this.$api.maintenanceIndex.getCouponIdByBuyDiscountsOrderId(data)
        this.couponId = res.data
      } catch (e) {
        //TODO handle the exception
      }
    },
    // 点击导航栏右侧
    onNavigationBarButtonTap() {
      uni.switchTab({
        url: '/pages/index/index'
      })
    },
    //我的爱车列表
    async GetCarList() {
      const value = uni.getStorageSync('Ztc_C_USER_INFO')
      if (value) {
        var ids
        ids = value.id
      }
      try {
        let data = {
          pageIndex: 1,
          pageSize: 10,
          uid: ids
        }
        const res = await this.$api.commodity.GetCarList(data)
        this.showPage = true
        this.mycarlist = res.data.list.slice(0, 5)
        if (res.data.total === 0) {
          this.defaultImg = true
        } else {
          this.defaultImg = false
        }
        this.carnumber = res.data.total
      } catch (e) {
        console.log(e)
      }
    },
    //我添加爱车
    async AddCar() {
      const value = uni.getStorageSync('Ztc_C_USER_INFO')
      if (value) {
        var ids
        ids = value.id
      }
      try {
        //添加爱车重复，退出添加
        this.mycarlist.forEach((item, index) => {
          if (item.car_Id === this.specifAllbrand.Configulist.id) {
            this.$refs.uToast.show({
              title: '选择的爱车重复了哦~',
              icon: false,
              type: 'warning',
            })
            throw new Error('EndIterative')
          }
        })
        let data = {
          uid: ids,
          brand_Id: this.specifAllbrand.brandlist.bid,
          brand_Name: this.specifAllbrand.brandlist.brand,
          car_Type_Id: this.specifAllbrand.Serieslist.carTypeId,
          car_Type_Name: this.specifAllbrand.Serieslist.carTypeName,
          car_Id: this.specifAllbrand.Configulist.id,
          car_Name: this.specifAllbrand.Configulist.name,
          car_Num: this.specifAllbrand.licenseplate, //车牌号
          mileage: 0, //里程 /km
          vin: '', //vin号
          engine: '', //发动机号
          displacement: this.specifAllbrand.Displacement.pName,
          yeartype: this.specifAllbrand.Modelyear.years
        }
        const res = await this.$api.common.AddCar(data)
        this.mycarlist = res.data.list
        this.GetCarList()
      } catch (e) {
        if (e.message !== 'EndIterative') throw e
      }
    },
    //删除我的爱车
    async DeleteCar(typeid) {
      this.flowershow = true
      try {
        let data = {
          id: typeid
        }
        const res = await this.$api.common.DeleteCar(data)
        if (res.code === 200) {
          this.flowershow = false
          this.GetCarList()
        }
      } catch (e) {
        console.log(e)
      }
    },
    //删除我的爱车
    deleteClick(typeid) {
      this.DeleteCar(typeid)
    },
    //设置默认
    async setdefauleClick(id) {
      try {
        let data = {
          loveCarId: id
        }
        const res = await this.$api.commodity.UpdateDefaultCar(data)
        uni.navigateBack({
          delta: 1
        })
      } catch (e) {
        console.log(e)
      }
      // this.UpdateDefaultCar(id)
    },
    async UpdateDefaultCar(id) {
      try {
        let data = {
          loveCarId: id
        }
        const res = await this.$api.commodity.UpdateDefaultCar(data)
        this.GetCarList()
      } catch (e) {
        console.log(e)
      }
    },

    navRedirect() {
      uni.redirectTo({
        url: '/pages_userModule/myCoupon/setMealDetail?id=' + this.couponId
      })
    },
    //绑定爱车
    async couponBindingCar(id) {

      let user = uni.getStorageSync('Ztc_C_USER_INFO')
      try {
        let data = {
          'couponId': this.couponId, //优惠券id
          'carNum': this.carNum, //车牌照
          'accountId': user.id //用户id
        }
        const res = await this.$api.maintenanceIndex.couponBindingCar(data)
        this.isWelfare = false
        console.log(res, '绑定')
        if (res.code === 200) {
          this.$u.toast('绑定成功')
          this.$u.debounce(this.navRedirect, 1500)
        } else {
          this.$u.toast(res.msg)
        }

      } catch (e) {

      }
    },
    //图片报错
    imageError: function(e) {
      if (e.type === 'error') {
        // this.derfimg = false
        // <image src="https://files.yzsheng.com/client/comparison/images/che.png"></image>
      }
    },
    addlovecar(index) {
      uni.navigateTo({
        url: '/pages_common/addYourCar/addYourCar?type=120&pageFrom=mylovecar'
      })
    },
    //我的爱车信息
    mycarinf(id, index, item) {
      this.carInfo = item
      this.carNum = item.car_Num
      if (this.type === '110') {
        // index 当前第几个车，current 第一层滑块定位检查记录3  state 检查记录滑块定位3
        uni.navigateTo({
          // url: '/pages_commodity/mylovecarInformation/mylovecarInformation?index=' + index + '&current=0' +
          //   '&state=3' + '&orderid=8bf34712c2d54010964af40bf0413734'
          url: '/pages_commodity/mylovecarInformation/mylovecarInformation?index=' + index +
            '&current=0' +
            '&state=3'
        })
      } else if (this.type === '100') {
        this.isWelfare = true
      } else {
        // this.$u.debounce(this.setdefauleClick(id), 2000)
        this.setdefauleClick(id)

        /* this.$refs.uToast.show({
          title: '切换成功',
          type: 'success',
          back: true
        }) */
      }
    },

    //获取福利购支付成功,根据订单id查询优惠券id
    getWelfareInfo(caridxix) {
      //#ifdef H5
      if (!this.isWxx) {
        this.getCouponIdByBuyDiscountsOrderId(caridxix.orderId)
      }

      //#endif
      this.couponId = caridxix.id
      this.welfareTitle = caridxix.title
    },
  }
}
